package com.cory.admin.sys.service;

import java.util.List;
import java.util.Map;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import com.cory.admin.sys.domain.entity.SysArea;
import com.cory.admin.sys.domain.portal.SysAreaVO;
import com.cory.admin.sys.mapper.SysAreaMapper;
import com.cory.boot.base.BaseService;
import com.cory.boot.component.cache.RedisUtils;
import com.cory.utils.lang.ChineseCharToEnUtil;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

/**
 * 区域表(数据来自京东) 服务实现类
 *
 * @author lw
 * @since 2022-01-23
 */
@Service
public class SysAreaService extends BaseService<SysAreaMapper, SysArea> {

    private String redis_areaTree_key = "sysAreaTreeJson";

    /**
     * 获取四级联动json数据
     */
    public Map<String,Object> getCityJsonData() {
        //省份
        List<SysArea> provinceList = baseMapper.selectByType("1");

        Map<String,Object> jsonMap = Maps.newLinkedHashMap();
        initProvince(jsonMap,provinceList);

        //市
        List<SysArea> cityList = baseMapper.selectByType("2");
        initArea(jsonMap,cityList);

        //区
        List<SysArea> countyList = baseMapper.selectByType("3");
        initArea(jsonMap,countyList);

        //街道
        List<SysArea> townList = baseMapper.selectByType("4");
        initArea(jsonMap,townList);

        return jsonMap;
    }

    /**
     * 构建AreaMap对象
     * @param jsonMap
     * @param areaList
     */
    @SuppressWarnings("unchecked")
    public void initArea(Map<String,Object> jsonMap, List<SysArea> areaList) {

        for(SysArea sysArea : areaList) {
            Map<String,String> subAreaMap = (Map<String,String>)jsonMap.get(sysArea.getParentId() + "");
            if(subAreaMap == null) {
                subAreaMap = Maps.newLinkedHashMap();
            }
            subAreaMap.put(sysArea.getCode(),sysArea.getName());
            jsonMap.put(sysArea.getParentId() + "",subAreaMap);
        }

    }

    /**
     * 构建省份
     * @param jsonMap
     * @param areaList
     */
    public void initProvince(Map<String,Object> jsonMap,List<SysArea> areaList) {
        List<Map<String,String>> agList = Lists.newLinkedList();
        List<Map<String,String>> hkList = Lists.newLinkedList();
        List<Map<String,String>> lsList = Lists.newLinkedList();
        List<Map<String,String>> tzList = Lists.newLinkedList();
        String ag = "abcdefg";
        String hk = "hijk";
        String ls = "lmnopqrs";
        String tz = "tuvwxyz";
        for(SysArea area : areaList) {
            Map<String,String> codeMap = Maps.newHashMap();
            codeMap.put("code",area.getCode());
            codeMap.put("address",area.getName());
            String firstLetter = ChineseCharToEnUtil.getFirstLetter(area.getName());
            if(ag.indexOf(firstLetter) != -1) {
                agList.add(codeMap);
                continue;
            } else if(hk.indexOf(firstLetter) != -1) {
                hkList.add(codeMap);
                continue;
            } else if(ls.indexOf(firstLetter) != -1) {
                lsList.add(codeMap);
                continue;
            } else if(tz.indexOf(firstLetter) != -1) {
                tzList.add(codeMap);
                continue;
            }
        }

        Map<String,Object> proviceMap = Maps.newLinkedHashMap();
        proviceMap.put("A-G",agList);
        proviceMap.put("H-K",hkList);
        proviceMap.put("L-S",lsList);
        proviceMap.put("T-Z",tzList);
        jsonMap.put("86",proviceMap);
    }

    /**
     * 获取区域树结构
     * @return
     */
    public List<SysAreaVO> getAreaTreeData() {
        List<SysAreaVO> provinceTreeList = RedisUtils.getList(redis_areaTree_key,SysAreaVO.class);
        if(provinceTreeList == null) {
            List<SysArea> provinceList = baseMapper.selectByParentId(0);
            provinceTreeList = Lists.newArrayList();
            for(SysArea s : provinceList) {
                SysAreaVO areaTree = new SysAreaVO();
                areaTree.setId(s.getId());
                if(s.getType() != null) {
                    areaTree.setLevel(Integer.valueOf(s.getType()));
                }
                areaTree.setName(s.getName());
                provinceTreeList.add(areaTree);

                buildAreaTree(areaTree);
            }
            RedisUtils.setList(redis_areaTree_key,provinceTreeList,-1);
        }

        return provinceTreeList;
    }


    /**
     * 构建区域树结构
     * @param areaVO
     * @return
     */
    public SysAreaVO buildAreaTree(SysAreaVO areaVO) {
        List<SysAreaVO> subTreeList = Lists.newArrayList();
        if(areaVO.getLevel() < 4) { //第4级不需要再递归查询
            List<SysArea> subAreaList = baseMapper.selectByParentId(areaVO.getId());
            if(CollectionUtils.isNotEmpty(subAreaList)) {
                subAreaList.forEach((s) -> {
                    SysAreaVO subAreaTree = new SysAreaVO();
                    subAreaTree.setId(s.getId());
                    if(s.getType() != null) {
                        subAreaTree.setLevel(Integer.valueOf(s.getType()));
                    }
                    subAreaTree.setName(s.getName());
                    //递归
                    subTreeList.add(buildAreaTree(subAreaTree));
                });
                areaVO.setAreaList(subTreeList);
            }
        }

        return areaVO;
    }

    /**
     * 重置树结构缓存
     * @return
     */
    public void resetAreaTreeData() {
        RedisUtils.del(redis_areaTree_key);
    }

}
